<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item style="font-size: 18px"><i class="el-icon-turn-off"></i>权限管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="success" @click="add()">添加</el-button>
            </div>
        <!--显示-->
        <el-table :data="tableData" style="width: 100%; margin-bottom: 20px" row-key="pId" border default-expand-all :tree-props="{ children: 'children' }">
            <!--显示-->
            <el-table-column prop="pTitle" label="权限名称" align="center">
            </el-table-column>
            <!--显示-->
            <el-table-column prop="pAction" label="权限标识符" align="center">
            </el-table-column>
            <!--URL路径-->
            <el-table-column prop="pApiUrl" label="URL路径" align="center">
            </el-table-column>
            <!--lcon名称-->
            <el-table-column prop="iconName" label="lcon名称" align="center">
            </el-table-column>
            <el-table-column prop="name" label="是否显示菜单">
                <template slot-scope="scope">
                    <span v-if="scope.row.isMenu == true">是</span>
                    <span v-if="scope.row.isMenu == false">否</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button size="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="text" icon="el-icon-delete" style="color: red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        </div>
        <!--添加-->
        <el-dialog title="添加" v-if="dialogVisible" :visible.sync="dialogVisible" width="30%">
            <el-form :model="ruleForm" :rules="rules" ref="addForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="权限名" prop="pTitle">
                    <el-input v-model="ruleForm.pTitle"></el-input>
                </el-form-item>
                <el-form-item label="上级权限" prop="pId">
                    <el-select v-model="ruleForm.parentId" placeholder="请选择上级菜单" :style="{ width: '100%', height: '100%' }">
                        <el-option style="height: 50%" value="00000000-0000-0000-0000-000000000000" label="根节点"></el-option>
                        <el-option style="height: 50%" value="">
                            <el-tree :data="tableData" :props="defaultProps" show-checkbox @check-change="handleCheckChange"> </el-tree>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="权限标识符" prop="pAction">
                    <el-input v-model="ruleForm.pAction"></el-input>
                </el-form-item>
                <el-form-item label="URL路径" prop="pApiUrl">
                    <el-input v-model="ruleForm.pApiUrl"></el-input>
                </el-form-item>
                <el-form-item label="lcon名称" prop="iconName">
                    <el-input v-model="ruleForm.iconName"></el-input>
                </el-form-item>
                <el-form-item label="排序号" prop="sortNo">
                    <el-input v-model="ruleForm.sortNo"></el-input>
                </el-form-item>
                <el-form-item label="是否菜单" prop="isMenu">
                    <el-switch v-model="ruleForm.isMenu" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addPermissionSubmit()">确 定</el-button>
            </span>
        </el-dialog>
        <!--修改-->
        <el-dialog title="修改" v-if="updatePermission" :visible.sync="updatePermission" width="50%">
            <el-form :model="ruleForm" :rules="rules" ref="editForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="权限名" prop="pTitle">
                    <el-input v-model="ruleForm.pTitle"></el-input>
                </el-form-item>
                <el-form-item label="上级权限" prop="pId">
                    <el-select v-model="ruleForm.parentId" placeholder="请选择上级菜单" :style="{ width: '100%', height: '100%' }">
                        <el-option style="height: 50%" value="00000000-0000-0000-0000-000000000000" label="根节点"></el-option>
                        <el-option style="height: 50%" value="">
                            <el-tree :data="tableData" :props="defaultProps" show-checkbox @check-change="handleCheckChange"> </el-tree>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="权限标识符" prop="pAction">
                    <el-input v-model="ruleForm.pAction"></el-input>
                </el-form-item>
                <el-form-item label="URL路径" prop="pApiUrl">
                    <el-input v-model="ruleForm.pApiUrl"></el-input>
                </el-form-item>
                <el-form-item label="lcon名称" prop="iconName">
                    <el-input v-model="ruleForm.iconName"></el-input>
                </el-form-item>
                <el-form-item label="排序号" prop="sortNo">
                    <el-input v-model="ruleForm.sortNo"></el-input>
                </el-form-item>
                <el-form-item label="是否菜单" prop="isMenu">
                    <el-switch v-model="ruleForm.isMenu" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="updatePermission = false">取 消</el-button>
                <el-button type="primary" @click="upt()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
    <script>
import { PermissionTree, addPermission,PermissionDelete,updatePermission,getPermission } from '../../api/permission.js';
// 导入树形选择组件
import Treeselect from '@riophae/vue-treeselect'
// 导入树形选择组件样式
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
    name:'permission',
    components: { Treeselect },
    data() {
        return {
            tableData: [],
            defaultProps: {
                children: 'children',
                label: 'pTitle',
                value: 'pId'
            },
            multipleSelection: [],
            defaultformValue: {
                pId: 0,
                pTitle: '',
                pAction: '',
                parentId: '',
                pApiUrl: '',
                isMenu: true,
                iconName: '',
                sortNo: 0
            },
            ruleForm: {
                pId: 0,
                pTitle: '',
                pAction: '',
                parentId: '',
                pApiUrl: '',
                isMenu: true,
                iconName: '',
                sortNo: 0
            },
            rules: {
                pTitle: [{ required: true, message: '请输入权限名', trigger: 'blur' }]
            },
            dialogVisible: false,
            updatePermission:false,
        };
    },
    created() {
        this.GetData();
    },
    methods: {
        GetData() {
            PermissionTree().then((res) => {
                console.log(res);
                this.tableData = res.data;
            });
        },
        //权限修改
        handleEdit(index, row) {
            console.log("123",index, row);
            this.updatePermission = true;
            getPermission(row.pId).then((res) => {
                console.log('反填数据', res);
                this.ruleForm = res.data;
            });
        },
        //修改确认
        upt() {
            this.$refs["editForm"].validate((valid) => {
            if (valid) {
                updatePermission(this.ruleForm).then((res) => {
                    console.log(res);
                    if (res.code == 200) {
                        this.$message.success('修改成功');
                        this.form = Object.assign({}, this.defaultformValue)  //将表单对象初始化清空
                        this.updatePermission = false;
                        this.GetData();
                    } else {
                        this.$message.success(res.message);
                    }
                });
            }
            else{}
            });
        },
        //权限删除
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {type: 'warning'}).then(() => {
                PermissionDelete(row.pId).then((res) => {
                    console.log(res);
                    if (res.code == 200) {
                        this.$message.success('删除成功!');
                        this.GetData();
                    } else {
                        this.$message.success(res.Message);
                    }
                });
            }).catch(() => {});
        },
        handleCheckChange(data, checked, indeterminate) {
            console.log(data, checked, indeterminate);
            this.multipleSelection = data;
            console.log('112', this.multipleSelection);
        },
        //权限添加
        add() {
            this.form = Object.assign({}, this.defaultformValue)  //将表单对象初始化清空
            this.dialogVisible = true;
        },
        //添加确认
        addPermissionSubmit() {
            this.$refs["addForm"].validate((valid) => {
            if (valid) {
                addPermission(this.form).then(res => {
                    if (res.code == 200) {
                        this.$message({message: '权限添加成功', type: 'success'});
                        this.form = Object.assign({}, this.defaultformValue)  //将表单对象初始化清空
                        this.dialogVisible = false;
                        this.show();
                    }
                    else {
                        this.$message({ message: '权限添加失败', type: 'warning'});
                        this.dialogVisible = false;
                    }
                })
            } else {
                console.log('数据验证失败！！');
                return false;
            }
            });
        },
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>